/*

This file is part of roomyak.

Copyright (C) 2008 Pixecur Technologies Incorporated (Esotera).
Visit Esotera at http://www.esoteras3.com
 
roomyak is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
 
roomyak is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with roomyak; see the file COPYING.  If not, write to 
Pixecur Technologies, 213-31 Peet Street, St. John's, NL, A1B 3W8. 
 
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
 
*/

package com.datasentinel.util.gnome;

import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.File;

import javax.imageio.ImageIO;

import com.datasentinel.util.thumbnail.ThumbnailException;
import com.datasentinel.util.thumbnail.ThumbnailProvider;

public class GTKThumbnailer implements ThumbnailProvider
{
	static
	{
		System.loadLibrary("GTKThumbnailer");
	}

	native static private byte[] thumbnail(String filename, boolean largeThumbnail);

	public GTKThumbnailer()
	{
		super();
	}

	public Image getSmallThumbnail(File file) throws ThumbnailException
	{
		return getThumbnail(file, false);
	}

	public Image getThumbnail(File file) throws ThumbnailException
	{
		return getThumbnail(file, true);
	}

	private Image getThumbnail(File file, boolean largeThumbnail) throws ThumbnailException
	{
		Image ans = null;
		try
		{
			byte[] imgBytes = GTKThumbnailer.thumbnail(file.getAbsolutePath(), true);
			if (null == imgBytes)
				throw new ThumbnailException("Error thumbnailing " + file.getAbsolutePath());
			ans = ImageIO.read(new ByteArrayInputStream(imgBytes));
		}
		catch (Exception e)
		{
			throw new ThumbnailException(e);
		}
		return ans;
	}
}
